//
//	_MiscMergeAskCommand.m -- "ask" merge command implementation
//		Written by Don Yacktman Copyright (c) 1995 by Don Yacktman.
//				Version 1.0.  All rights reserved.
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	


#import <misckit/miscmerge.h>
#import "_MiscMergeAskCommand.h"
#import "_MiscMergeQuery.h"

@implementation _MiscMergeAskCommand

static MiscString *myKey = nil;

- parseFromString:(MiscString *)aString
{
	MISC_Merge_Cond_Op operator;
	if (!myKey) {
		myKey = [MiscString new];
		[myKey setStringValue:"ask"];
	}
	[self eatKeyWord:myKey from:aString isOptional:NO];
	value1 = [self getArgumentStringFrom:aString toEnd:NO];
	operator = [self getConditionalFrom:aString];
	if (operator != MISC_MCO_EQUAL) {
		id tempString = [MiscString
				newWithString:"Set requires operator to be an =."];
		[self error_conditional:tempString];
		[tempString free];
		value2 = [MiscString new];
	}
	value2 = [self getPromptFrom:aString toEnd:YES];
	[value1 trimWhiteSpaces];
	return self;
}


- executeForMerge:(MiscMergeEngine *)aMerger
{
	_MiscMergeQuery *query = [_MiscMergeQuery new];
	id question = [MiscString new];
	if (([value2 length] < 1) || ![value2 stringValue]
				|| (!(*([value2 stringValue])))) {
		// This needs to be localized!
		[question setFromFormat:
				"Please enter the value for the %s field:", value1];
	} else { // Use the question that was given to us
		[question setStringValue:[value2 stringValue]];
	}
	[query setQuestion:[question stringValue]];
	[query showWindow:self];
	if ([query status] == MISC_QueryAccepted) {
		id temp  = [MiscString new];
		[temp setStringValue:[query stringValue]];
		[temp trimWhiteSpaces];
		[aMerger setSymbol:value1 toValue:temp];
		[temp free];
	} else if ([query status] == MISC_QueryCancelled) {
		// Cancelling is treated as an "omit"!
		[aMerger abortMerge];
		[question free];
		return nil;
	}
	[question free];
	return self;
}

@end
